package com.dtsmoll.permissions;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;

public class PermissionsUtils {

    static OnPermissionsCallback locationCallback;

    static String[] permissions;

    /**
     * 判断权限
     */
    public static boolean checkSelfPermission(Activity activity, String[] permissions) {
        boolean status = true;
        for (int i = 0; i < permissions.length; i++) {
            if (ActivityCompat.checkSelfPermission(activity, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                status = false;
                break;
            }
        }
        return status;
    }

    /**
     * 获取地址信息
     */
    public static void requestPermissions(Activity activity, String[] permissions, OnPermissionsCallback callback) {
        PermissionsUtils.locationCallback = callback;
        PermissionsUtils.permissions = permissions;
        // 判断当前是否有权限
        if (!checkSelfPermission(activity, permissions)) {
            activity.startActivity(new Intent(activity, PermissionsActivity.class));
        } else {
            PermissionsUtils.locationCallback.onCallback(true);
        }
    }

    public interface OnPermissionsCallback {
        void onCallback(boolean status);
    }
}
